package com.lanxin.jet.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.lanxin.jet.services.UserService;
import com.lanxin.jet.tools.JetDate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/home")
public class HomeController {

    @Autowired
    private UserService userService;

    /**
     * 后台首页面
     * @param request
     * @param map
     * @return
     */
    @RequestMapping("")
    public String index(HttpSession session, ModelMap map) {
        if (session.getAttribute("uid") == null) {
            return "admin/login";
        } else {
            map.put("datetime", JetDate.dateNow(null));
            map.put("timestamp", JetDate.timeNow());
            return "home/index";
        }
    }

    /**
     * 栏目管理页面
     * @param session
     * @return
     */
    @RequestMapping("/channel")
    public String channel(HttpSession session) {
        if (userService.checkRole(session, UserService.ROLE_CONTENT)) {
            return "home/channel";
        } else {
            return "home/no_role";
        }
    }

    /**
     * 内容管理页面
     * @param request
     * @param id
     * @param map
     * @return
     */
    @RequestMapping("/content/id/{id}")
    public String content(HttpServletRequest request, @PathVariable String id, ModelMap map) {
        if (userService.checkRole(request.getSession(), UserService.ROLE_CONTENT)) {
            map.put("cid", id);
            return "home/content";
        } else {
            return "home/no_role";
        }
    }

    /**
     * 资源管理页面
     * @param session
     * @param map
     * @return
     */
    @RequestMapping("/resource")
    public String resource(HttpServletRequest request, ModelMap map) {
        if (userService.checkRole(request.getSession(), UserService.ROLE_RESOURCE)) {
            map.put("bid", request.getParameter("bid"));
            map.put("entry", request.getParameter("entry"));
            return "home/resource";
        }
        return "home/no_role";
    }

    /**
     * 评论管理页面
     * @param session
     * @return
     */
    @RequestMapping("/comment")
    public String comment(HttpServletRequest request, ModelMap map) {
        if (userService.checkRole(request.getSession(), UserService.ROLE_CONTENT)) {
            map.put("bid", request.getParameter("bid"));
            return "home/comment";
        }
        return "home/no_role";
    }

    /**
     * 用户管理页面
     * @param session
     * @return
     */
    @RequestMapping("/user")
    public String user(HttpSession session) {
        if (userService.checkRole(session, UserService.ROLE_USER)) {
            return "home/user";
        }
        return "home/no_role";
    }

}